#import <AdSupport/AdSupport.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import "AppDelegate.h"
#import "GeneratedPluginRegistrant.h"
#import "GoumYourPlugin.h"

@implementation AppDelegate

- (BOOL)              application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self gmToRequestIdfa];
    GoumYourPlugin.plugin.controller = (FlutterViewController *)self.window.rootViewController;
    [GoumYourPlugin registerWithRegistrar:[self registrarForPlugin:@"gm_app_channel"]];
    [GeneratedPluginRegistrant registerWithRegistry:self];
    // Override point for customization after application launch.
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (void)gmToRequestIdfa {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSString *mIdfa = @"00456800-0340-0570-0560-024087010670";
        if (@available(iOS 14, *)) {
            [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
                switch (status) {
                    case ATTrackingManagerAuthorizationStatusAuthorized:
                        // 用户允许跟踪，可以获取并使用IDFA
                        {
                            NSString *idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
                            [self gmToUserIDFA:idfa];
                            // 使用 idfa 的代码...
                        }
                        break;

                    case ATTrackingManagerAuthorizationStatusDenied:
                    case ATTrackingManagerAuthorizationStatusRestricted:
                    case ATTrackingManagerAuthorizationStatusNotDetermined:
                        // 对于这些状态，我们将IDFA视为限制或不可用，并将其设置为全零。
                        {
                            NSString *idfa = mIdfa;
                            [self gmToUserIDFA:idfa];
                            // 使用 idfa 的代码...
                        }
                        break;

                    default:
                        // 未知的状态，按照您的要求设置为全零。
                        {
                            NSString *idfa = mIdfa;
                            [self gmToUserIDFA:idfa];
                            // 使用 idfa 的代码...
                        }
                        break;
                }
            }];
        } else {
            // iOS 14以下版本
            BOOL trackingEnabled = [[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled];
            NSString *idfa;

            if (trackingEnabled) {
                idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
            } else {
                // 用户限制了广告跟踪
                idfa = mIdfa;
            }

            // 使用 idfa 的代码...
        }
    });
}

- (void)gmToUserIDFA:(NSString *)value {
    
}

@end
